#pragma once

#include "parentpanel.h"
#include "zoomviewpanel.h"
#include "toolbar.h"

class EditorPanel : public ParentPanel
{
public:
	/** this method will be called just after the new data is loaded */
	typedef void (*NOTIFY_LOAD)(void * a_toNotify, EditorPanel * a_whichPanel);
protected:
	char * m_filename;
	ZoomViewPanel * m_zoomViewPanel;
	ParentPanel m_zoomViewParent;
	ToolBar m_toolbar;
	NOTIFY_LOAD m_notifier;
	void * m_whoToNotify;
public:
	ZoomViewPanel * getZoomViewContent(){return m_zoomViewPanel;}
protected:
	static void notifyToolUse(void * a_tool)
	{
		Tool * tool = (Tool*)a_tool;
		EditorPanel* ep = (EditorPanel*)tool->getContext();
		ZoomViewPanel * zv = ep->getZoomViewContent();
		Tool * oldTool = zv->getTool();
		// if the last tool was on, turn it off.
		if(oldTool && oldTool->isOn())
		{
			oldTool->deactivate();
		}
		zv->setTool((tool->isOn())?tool:0);
		zv->setDirty(0);
	}
public:
	void setLoadNotifier(void * a_whoToNotify, NOTIFY_LOAD a_notifier)
	{
		m_whoToNotify=a_whoToNotify;
		m_notifier=a_notifier;
	}
	EditorPanel():m_filename(0),m_zoomViewPanel(0),m_notifier(0)
	{
		addChild(&m_toolbar);
		addChild(&m_zoomViewParent);
	}
	const char * getFileName(){return m_filename;}
	void setFileName(const char * a_filename)
	{
		if(m_filename)
			delete m_filename;
		m_filename=strCpy(a_filename);
	}
	void setZoomViewPanel(ZoomViewPanel * a_zoomViewPanel)
	{
		if(m_zoomViewPanel)
		{
			int index = m_children.indexOf(m_zoomViewPanel);
			m_children.set(index, a_zoomViewPanel);
		}
		else
		{
			m_zoomViewParent.addChild(a_zoomViewPanel);
		}
		m_zoomViewPanel = a_zoomViewPanel;
		m_zoomViewPanel->getArea()->setPosition(m_zoomViewParent.getArea()->getPosition());
	}
	static int indexOfLetter(const char * a_str, const char & a_letter, const int & a_start);
	static int indexOfLetterLast(const char * a_str, const char & a_letter, const int & a_end);
	static int parseInt(const char * a_str, const int & len, int & a_index);
	static void fileDialog(const char * a_title, char* a_startPath, char* a_filter, char * a_out_filePath, 
				const int & a_filePathMaxSize, const char * a_default);
	static const char * getFilenameFromPath(const char * a_path, char * a_out_name, const int & a_outSize);
	char * strCpy(const char * a_str)
	{
		if(!a_str)return 0;
		int len = strlen(a_str)+1;
		char * str = new char[len];
		for(int i = 0; i < len; ++i)str[i]=a_str[i];
		return str;
	}
	/** a method to initialize the UI of this editor. */
	virtual void initUI(Panel * a_parent)=0;
	/** @return true if method saved correctly, false if method did not save */
	virtual bool saveMethod(const char * a_path)=0;
	/** @return true if method loaded correctly, false if method did not load. old data should remain if false... */
	virtual bool loadMethod(const char * a_path)=0;
	/** uses file dialog to determine what to save() */
	virtual bool save();
	/** uses file dialog to determine what to load() */
	virtual bool load();
	/** calls saveMethod */
	bool save(const char * a_path);
	/** calls loadMethod */
	bool load(const char * a_path);

	void release()
	{
		m_toolbar.release();
		if(m_filename){delete m_filename;m_filename=0;}
	}
	void setArea(const SRect & a_area)
	{
		// TODO magic number
		int size = getMasterPanel()->getImageClip(0).clip.getHeight();
		ParentPanel::setArea(a_area);
		// rectangle for the toolbar at the top of the editor
		SRect toolBarD(a_area);
		toolBarD.setHeight(size);
		m_toolbar.setArea(toolBarD);
		// rectangle for the res of the editor area
		SRect allOtherArea(a_area);
		allOtherArea.setMinY(toolBarD.getMaxY());
		if(!m_zoomViewParent.getArea()->equals(allOtherArea))
		{
			m_zoomViewParent.setArea(allOtherArea);
			if(m_zoomViewPanel)
			{
				m_zoomViewPanel->getArea()->setPosition(m_zoomViewParent.getArea()->getPosition());
			}
		}
	}
	void setTools(ToolBar::ToolData * a_toolData, const int & a_numTools)
	{
		m_toolbar.addTools(a_numTools, a_toolData, this, notifyToolUse);
		m_zoomViewParent.setChildrenScrollable(true);
		m_zoomViewParent.setNullFocusAllowed(false);
	}

	void updateTabText(const char * a_text);

	void updateTabTextFromPath(const char * a_path);
};